﻿using System;
using System.Net;
using System.Windows;

using Microsoft.Phone.Controls;

using Coding4Fun.Toolkit.Controls.Common;

using Newtonsoft.Json;

namespace Coding4FunHueWP
{
    public partial class MainPage : PhoneApplicationPage
    {
		// Constructor
        public MainPage()
        {
            InitializeComponent();
       
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void RegisterButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var client = new WebClient();

                //our uri to perform registration
                var uri = new Uri(string.Format("http://{0}/api", _hostnameTextBox.Text));

                //create our registration object, along with username and description
                var reg = new
                {
                    username = _usernameTextBox.Text,
                    devicetype = "Coding4Fun Hue Light Project"
                };

                var jsonObj = JsonConvert.SerializeObject(reg);


                //decide what to do with the response we get back from the bridge
                client.UploadStringCompleted += (o, args) => Dispatcher.BeginInvoke(() =>
                {
                    try
                    {
                        _responseTextBox.Text = args.Result;
                    }
                    catch (Exception ex)
                    {
                        _responseTextBox.Text = ex.Message;
                    }
                });

                //Invoke a POST to the bridge
                client.UploadStringAsync(uri, jsonObj);
            }
            catch (Exception ex)
            {
                _responseTextBox.Text = ex.Message;
            }
           

        }

        private void GetLightsButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var client = new WebClient();

                //build our uri to retreive config and bulbs from our bridge
                var uri = new Uri(string.Format("http://{0}/api/{1}/", _hostnameTextBox.Text, _usernameTextBox.Text));

                //decide what to do with the response we get back from the bridge
                client.DownloadStringCompleted += (o, args) => Dispatcher.BeginInvoke(() =>
                {
                    try
                    {
                        _responseTextBox.Text = args.Result;
                    }
                    catch (Exception ex)
                    {
                        _responseTextBox.Text = ex.Message;
                    }
                   
                });

                //query the bridge
                client.DownloadStringAsync(uri);
            }
            catch (Exception ex)
            {
                _responseTextBox.Text = ex.Message;
            }
            
        }

        private void SetColorButtonClick(object sender, RoutedEventArgs e)
        {

            try
            {
                //Get the HSV Value from the currently selected color
                var hsv = _colorPicker.Color.GetHSV();

                //build our State object
                var state = new
                {
                    on = true,
                    hue = (int)(hsv.Hue * 182.04), //we convert the hue value into degrees by multiplying the value by 182.04
                    sat = (int)(hsv.Saturation * 254)
                };

                //convert it to json:
                var jsonObj = JsonConvert.SerializeObject(state);

                //set the api url to set the state
                var uri = new Uri(string.Format("http://{0}/api/{1}/lights/{2}/state", _hostnameTextBox.Text, _usernameTextBox.Text, _lightIndexTextBox.Text));

                var client = new WebClient();

                //decide what to do with the response we get back from the bridge
                client.UploadStringCompleted += (o, args) => Dispatcher.BeginInvoke(() =>
                {
                    try
                    {
                        _responseTextBox.Text = args.Result;
                    }
                    catch (Exception ex)
                    {
                        _responseTextBox.Text = ex.Message;
                    }

                });

                //Invoke the PUT method to set the state of the bulb
                client.UploadStringAsync(uri, "PUT", jsonObj);
            }
            catch (Exception ex)
            {
                _responseTextBox.Text = ex.Message;
            }
        }
    }
}